package cn.bdqn.pn.controller.base;

import cn.bdqn.pn.pojo.base.Coupon;
import cn.bdqn.pn.pojo.commodity.Commodity;
import cn.bdqn.pn.pojo.commodity.Showtype;
import cn.bdqn.pn.service.base.CouponService;
import cn.bdqn.pn.service.commodity.CommodityService;
import cn.bdqn.pn.service.commodity.ShowTypeService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/CouponController")
public class CouponController {
    @Autowired
    private CouponService couponService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private ShowTypeService showTypeService;
    @RequestMapping("/index")
    public String index(){
        return "shops_index";
    }


    /**
     * 分页全部
     * @return
     */
    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public String getAll(Coupon coupon,Integer pageNo,Integer size,Model model){
        System.out.println();
        Page<Coupon> couponList=couponService.getAllCouponList(coupon,pageNo,size);
        System.out.println(coupon.getCouponname()+"++++++++++++++++++++++++++++3");
        System.out.println(coupon.getCoupontype()+"++++++++++++++++++++++++++++");
        System.out.println(coupon.getStatus()+"++++++++++++++++++++++++++++");
        System.out.println(coupon.getDisable()+"++++++++++++++++++++++++++++");
        model.addAttribute("couponList",couponList);
        model.addAttribute("CouponName",coupon.getCouponname());
        model.addAttribute("CouponType",coupon.getCoupontype());
        model.addAttribute("Status",coupon.getStatus());
        model.addAttribute("Disable",coupon.getDisable());
        System.out.println(JSON.toJSONString(couponList,true));
        return "Coupon";

       /* Page<Coupon> co=couponService.getAllCoupon(pageable);
        couponList=co.getContent();*/
     /*   for (Coupon coupon: couponList
             ) {
            long lo=coupon.getEndtime().getTime();//将时间戳转换为long
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是你要转成后的时间的格式
            String sd=sdf.format(lo);
            System.out.println(sd);
        }*/
    }

    /**
     * 进入添加页面
     * @return
     */
    @RequestMapping("showAdd")
    public String showAdd(Model model){
        //获取所有类型
        List<Showtype> showtypeList = showTypeService.ShowTypelist();
        //获取所有商品
        List<Commodity> commodityList=commodityService.findAllCommdity(1);
        model.addAttribute("showtypelist",showtypeList);
        model.addAttribute("commditylist",commodityList);
        return "add_Coupon";
    }

    /**
     * 添加一条优惠券
     * @return
     */

    @RequestMapping("/addCoupon")
    @ResponseBody
    public Coupon addCoupon(Coupon coupon,Integer iddd){
        System.out.println(iddd+"ljgffhbjknlcgjblkn;gfzdhgjbjlkghgxdfzhjk");
        System.out.println(coupon.getReleid()+"ljgffhbjknlcgjblkn;gfzdhgjbjlkghgxdfzhjk");
        if(coupon.getReleid()==null){
            coupon.setReleid(1);
        }
        coupon.setDisable(0);
        coupon.setStatus(1);
        coupon=couponService.addCoupon(coupon);
        return coupon;
    }

    /**
     *
     * @param id
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public void deleteById(Integer id,Model model){
        Coupon coupon= (Coupon) couponService.findaddById(id);
        coupon.setDisable(1);
        couponService.updateCoupon(coupon);
    }
}
